package com.example.farmeri.pojo.vo;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.farmeri.tools.ResponseUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class EvenBusSuccess implements Callback {

    @Override
    public void onFailure(Call call, IOException e) {
        Log.e("Callback", "onFailure: " + e.getMessage());
        EventBus.getDefault().post("网络请求失败");
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if (response.code()>=200 && response.code()<300){
            String body = response.body().string();
            ResponseUtils responseUtils =  JSON.parseObject(body, ResponseUtils.class);
            if (responseUtils.getCode() == 200){
                EventBus.getDefault().post("success");
            }else {
                EventBus.getDefault().post(responseUtils.getMessage());
            }
        }
    }

}
